package top.drinyeit.weilong.presenter;

import top.drinyeit.weilong.activity.RechargeActivity;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;
import top.drinyeit.weilong.util.SPUtil;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/6.
 */

public class RechargePresenter {
    private RechargeActivity activity;
    public RechargePresenter(RechargeActivity activity){
        this.activity = activity;
    }
    //充值
    public void recharge(String card_number, String card_password) {
        String user_id = SPUtil.getShareString(Constant.USER_ID);
        activity.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                activity.dismissLoading();
                activity.showToastMsg(o.getMessage());
                if (o.getCode() == COMMON_SUCCESS) {
                    activity.setResult(1);
                    activity.finish();
                }
            }
        });
        RetrofitMethod.getInstance().recharge(subscriber, user_id,card_number,card_password);
    }
}
